<?php

namespace App\Console\Commands;

use App\Models\School;
use App\Models\User;
use App\Services\FaceService;
use App\TeSchool\SmartCampus;
use App\TxSchool\Type\Type;
use Illuminate\Console\Command;
class UserImages extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'user:faces';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '腾讯人脸照片';


    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * @throws \App\TxSchool\Exceptions\UnknownTypeException
     */
    public function handle()
    {
        $schools = School::isActive()->get();
        $service = new FaceService();
        foreach ($schools as $school) {
            $smartCampus = new SmartCampus([
                'objectid' => $school->objectid,
                'devCode' => $school->dev_code,
                'objType' => Type::OBJECT_TYPE_SCHOOL,
                'key' => $school->key,
            ], ['developer', 'school']);
            $smartCampus->school->setTypeSchool();
            $users = User::query()->where("school_id", $school->id)->get();
            foreach ($users as $user){
                if($user->userid){
                    $res = $smartCampus->school->getFaceImage($user->userid);
                    if($res['code'] == 0) {
                        dump($res);
                        if(isset($res['data'])){
                            $data = $res['data'][0];
                            if($data['face1']){
//                                $txUser = $service->getPerson($user->id);
//                                if($user){
//                                    $service->deletePerson($user->$txUser);
//                                }
                                $service->createPerson($user->id, $data['face1']);
                            }
                            if($data['face2']){
                                $service->addFaceByUrl($user->id,$data['face2']);
                            }
                        }
                    }
                }
            }
        }
    }
}
